const shoppingCar = {};

/**
 * 获取
 */ shoppingCar.getInfo = function(cartInfo) {
    var cart;
    var cart_count;
    if (!cartInfo) {
        cart = wx.getStorageSync("cart") || [];
        cart_count = wx.getStorageSync("cart_count") || 0;
    } else {
        cart = cartInfo.cart;
        cart_count = cartInfo.cart_count;
    }
    var statisticsArry = [];
    cart.forEach((item, index) => {
        //遍历购物车
        var newItem = {
            product_id: item.product_id,
            count: item.count
        };
        if (statisticsArry.length == 0) {
            statisticsArry.push(newItem);
        } else {
            var num = 0;
            for (var i = 0; i < statisticsArry.length; i++) {
                if (item.product_id == statisticsArry[i].product_id) {
                    num++;
                    statisticsArry[i] = {
                        product_id: statisticsArry[i].product_id,
                        count: statisticsArry[i].count + item.count
                    };
                    break;
                }
            }
            if (num == 0) {
                statisticsArry.push(newItem);
            }
        }
        // console.log('-------------------++++++++++++++++++++++++++')
        // console.log(statisticsArry);
        });
    return {
        cart: cart,
        cart_count: cart_count,
        statisticsArry: statisticsArry
    };
}
/**
 * 获取-接龙
 */;

shoppingCar.getDragonInfo = function(cartInfo, group_id) {
    var cart;
    var cart_count;
    if (!cartInfo) {
        cart = wx.getStorageSync(`dragon_cart_${group_id}`) || [];
        cart_count = wx.getStorageSync(`dragon_cart_count_${group_id}`) || 0;
    } else {
        cart = cartInfo.cart;
        cart_count = cartInfo.cart_count;
    }
    var statisticsArry = [];
    cart.forEach((item, index) => {
        //遍历购物车
        var newItem = {
            product_id: item.product_id,
            count: item.count
        };
        if (statisticsArry.length == 0) {
            statisticsArry.push(newItem);
        } else {
            var num = 0;
            for (var i = 0; i < statisticsArry.length; i++) {
                if (item.product_id == statisticsArry[i].product_id) {
                    num++;
                    statisticsArry[i] = {
                        product_id: statisticsArry[i].product_id,
                        count: statisticsArry[i].count + item.count
                    };
                    break;
                }
            }
            if (num == 0) {
                statisticsArry.push(newItem);
            }
        }
    });
    return {
        cart: cart,
        cart_count: cart_count,
        statisticsArry: statisticsArry
    };
}
/**
 * 加
 */;

shoppingCar.addPro = function(proInfo) {
    var cart = wx.getStorageSync("cart") || [];
    var cart_count = wx.getStorageSync("cart_count") || 0;
    var index = -1;
    if (cart.length == 0) {
        cart.push(proInfo);
    } else {
        for (var i = 0; i < cart.length; i++) {
            if (cart[i].id == proInfo.id) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            cart[index].count = cart[index].count + proInfo.count;
        } else {
            cart.push(proInfo);
        }
    }
    //保存
        shoppingCar.save(cart, cart_count + proInfo.count);
    //获取购物车信息
        let cartInfo = {
        cart: cart,
        cart_count: cart_count + proInfo.count
    };
    // console.log('00000000000');
    // console.log(cartInfo);
        return shoppingCar.getInfo(cartInfo);
}
/**
 * 
 * @param {接龙-加} proInfo 
 */;

shoppingCar.addDragonPro = function(proInfo, group_id) {
    var cart = wx.getStorageSync(`dragon_cart_${group_id}`) || [];
    var cart_count = wx.getStorageSync(`dragon_cart_count_${group_id}`) || 0;
    var index = -1;
    if (cart.length == 0) {
        cart.push(proInfo);
    } else {
        // for (var i = 0; i < cart.length; i++) {
        //   if (cart[i].id == proInfo.id) {
        //     index = i;
        //     break;
        //   }
        // }
        // console.log(index)
        cart.forEach((item, curr_index) => {
            if (item.id == proInfo.id) index = curr_index;
        });
        if (index != -1) {
            cart[index].count = cart[index].count + proInfo.count;
        } else {
            cart.push(proInfo);
        }
    }
    //保存
        shoppingCar.saveDragon(cart, cart_count + proInfo.count, group_id);
    //获取购物车信息
        let cartInfo = {
        cart: cart,
        cart_count: cart_count + proInfo.count
    };
    return shoppingCar.getDragonInfo(cartInfo, group_id);
}
/**
 * 单属性商品减
 */;

shoppingCar.reducePro = function(proArry) {
    //proArry,商品属性id数组
    var cart = wx.getStorageSync("cart") || [];
    var cart_count = wx.getStorageSync("cart_count") || 0;
    for (var i = 0; i < cart.length; i++) {
        if (cart[i].product_id == proArry[0]) {
            // if(item.count==1){
            //   wx.showToast({
            //     title: '该商品不能减少了哟~.~',
            //     icon:"none"
            //   })
            // }else if(item.count>1){
            //   item.count--;
            //   cart_count--
            // }
            if (cart[i].count > 1) {
                cart[i].count--;
                cart_count--;
                break;
            } else {
                cart[i].count = 0;
                cart_count--;
                cart.splice(i, 1);
                break;
            }
        }
    }
    shoppingCar.save(cart, cart_count);
    return shoppingCar.getInfo({
        cart: cart,
        cart_count: cart_count
    });
}
/**
 * 单属性商品减-接龙
 */;

shoppingCar.reduceDragonPro = function(proArry, group_id) {
    //proArry,商品属性id数组
    var cart = wx.getStorageSync(`dragon_cart_${group_id}`) || [];
    var cart_count = wx.getStorageSync(`dragon_cart_count_${group_id}`) || 0;
    for (var i = 0; i < cart.length; i++) {
        if (cart[i].product_id == proArry[0]) {
            if (cart[i].count > 1) {
                cart[i].count--;
                cart_count--;
                break;
            } else {
                cart[i].count = 0;
                cart_count--;
                cart.splice(i, 1);
                break;
            }
        }
    }
    shoppingCar.saveDragon(cart, cart_count, group_id);
    return shoppingCar.getDragonInfo({
        cart: cart,
        cart_count: cart_count
    }, group_id);
}
/**
 * 保存
 */;

shoppingCar.save = function(cart, cartCount) {
    wx.setStorageSync("cart", cart);
    wx.setStorageSync("cart_count", cartCount);
}
/**
 * 保存-接龙
 */;

shoppingCar.saveDragon = function(cart, cartCount, group_id) {
    wx.setStorageSync(`dragon_cart_${group_id}`, cart);
    wx.setStorageSync(`dragon_cart_count_${group_id}`, cartCount);
}
/**
 * 删除
 */;

shoppingCar.delete = function(proArry) {
    //proArry,商品属性id数组
    var cart = wx.getStorageSync("cart") || [];
    var cart_count = wx.getStorageSync("cart_count") || 0;
    var newCart = [];
    if (cart.length > 0) {
        cart.forEach((item, index) => {
            let num = 0;
            for (var i = 0; i < proArry.length; i++) {
                if (item.id == proArry[i]) {
                    num++;
                    cart_count -= item.count;
                    break;
                }
            }
            if (num == 0) {
                newCart.push(item);
            }
        });
    }
    //保存
        shoppingCar.save(cart, cart_count);
    //获取购物车信息
        let cartInfo = {
        cart: cart,
        cart_count: cart_count
    };
    return shoppingCar.getInfo(cartInfo);
};

shoppingCar.statisticsInfo = function() {};

module.exports = shoppingCar;